package cn.wzut.edut.dao.inf;

import cn.wzut.edut.dao.SnowflakeIdWorker;
import cn.wzut.edut.dao.mapper.PtyStuGroupMapper;
import cn.wzut.edut.dao.mapper.PtyStuGroupRelMapper;
import cn.wzut.edut.dao.mapper.PtyTeacherGroupMapper;
import cn.wzut.edut.dao.mapper.PtyTeacherGroupRelMapper;
import cn.wzut.edut.dao.pojo.table.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * 学生教师组管理
 */
@Repository
public class Group {
    @Autowired
    private PtyStuGroupMapper ptyStuGroupMapper;
    @Autowired
    private PtyStuGroupRelMapper ptyStuGroupRelMapper;
    @Autowired
    private PtyTeacherGroupMapper ptyTeacherGroupMapper;
    @Autowired
    private PtyTeacherGroupRelMapper ptyTeacherGroupRelMapper;
    /**
     * 查询所有学生组
     */
    public List<PtyStuGroup> selectAllStuGroup(){return ptyStuGroupMapper.selectList(null);}
    /**
     * 查询所有教师组
     */
    public List<PtyTeacherGroup> selectAllTeacherGroup(){return ptyTeacherGroupMapper.selectList(null);}
    /**
     * 按学生组id返回学生学生组关系表数（一个学生组多少人）
     */
    public long countStuGroup(){return ptyStuGroupMapper.selectCount(null);}
    /**
     * 按教师组id返回教师教师组关系表数（一个教师组多少人）
     */
    public long countTeacherGroup(){return ptyTeacherGroupMapper.selectCount(null);}
    /**
     * 按学生id查询学生学生组关系表
     */
    public List<PtyStuGroupRel> selectStuGroupRelInStu(String stu_id){
        QueryWrapper<PtyStuGroupRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("stu_id",stu_id);
        return ptyStuGroupRelMapper.selectList(queryWrapper);
    }
    /**
     * 按学生组id查询学生学生组关系表
     */
    public List<PtyStuGroupRel> selectStuGroupRelInGroup(String group_id){
        QueryWrapper<PtyStuGroupRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("stu_group_id",group_id);
        return ptyStuGroupRelMapper.selectList(queryWrapper);
    }
    /**
     * 按学生组id查找学生组
     */
    public PtyStuGroup selectStuGroup(String group_id){
        QueryWrapper<PtyStuGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",group_id);
        return ptyStuGroupMapper.selectOne(queryWrapper);
    }
    /**
     * 按教师id查找学生组
     */
    public List<PtyStuGroup> selectStuGroupInTeacher(String teacher_id){
        QueryWrapper<PtyStuGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teacher_id",teacher_id);
        return ptyStuGroupMapper.selectList(queryWrapper);
    }
    /**
     * 按教师id返回学生组数
     */
    public long countStuGroupInTeacher(String teacher_id){
        QueryWrapper<PtyStuGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teacher_id",teacher_id);
        return ptyStuGroupMapper.selectCount(queryWrapper);
    }
    /**
     * 按教师组id查询教师组
     */
    public PtyTeacherGroup selectTeacherGroup(String group_id){
        QueryWrapper<PtyTeacherGroup> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",group_id);
        return ptyTeacherGroupMapper.selectOne(queryWrapper);
    }
    /**
     * 按教师id查询教师教师组关系表
     */
    public List<PtyTeacherGroupRel> selectTeacherGroupRelInTeacher(String teacher_id){
        QueryWrapper<PtyTeacherGroupRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teacher_id",teacher_id);
        return ptyTeacherGroupRelMapper.selectList(queryWrapper);
    }
    /**
     * 按教师组id查询教师教师组关系表
     */
    public List<PtyTeacherGroupRel> selectTeacherGroupRelInGroup(String group_id){
        QueryWrapper<PtyTeacherGroupRel> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("teacher_group_id",group_id);
        return ptyTeacherGroupRelMapper.selectList(queryWrapper);
    }
    /**
     * 增加学生组
     */
    public String insertPtyStuGroup(PtyStuGroup ptyStuGroup){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyStuGroup.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyStuGroupMapper.insert(ptyStuGroup);
        }catch (Exception e){
            return ptyStuGroup.getId();
        }
        return ptyStuGroup.getId();
    }
    /**
     * 增加教师组
     */
    public String insertPtyTeacherGroup(PtyTeacherGroup ptyTeacherGroup){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyTeacherGroup.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyTeacherGroupMapper.insert(ptyTeacherGroup);
        }catch (Exception e){
            return ptyTeacherGroup.getId();
        }
        return ptyTeacherGroup.getId();
    }
    /**
     * 增加学生学生组表
     */
    public String insertPtyStuGroupRel(PtyStuGroupRel ptyStuGroupRel){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyStuGroupRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyStuGroupRelMapper.insert(ptyStuGroupRel);
        }catch (Exception e){
            return ptyStuGroupRel.getId();
        }
        return ptyStuGroupRel.getId();
    }
    /**
     * 增加教师教师组表
     */
    public String insertPtyTeacherGroupRel(PtyTeacherGroupRel ptyTeacherGroupRel){
        try {
            SnowflakeIdWorker snowflakeIdWorker = new SnowflakeIdWorker();
            ptyTeacherGroupRel.setId(String.valueOf(snowflakeIdWorker.nextId()));
            ptyTeacherGroupRelMapper.insert(ptyTeacherGroupRel);
        }catch (Exception e){
            return ptyTeacherGroupRel.getId();
        }
        return ptyTeacherGroupRel.getId();
    }
    /**
     * 修改学生组
     */
    public boolean updatePtyStuGroup(PtyStuGroup ptyStuGroup){
        try {
            QueryWrapper<PtyStuGroup> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ptyStuGroup.getId());
            ptyStuGroupMapper.update(ptyStuGroup,queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 修改教师组
     */
    public boolean updatePtyTeacherGroup(PtyTeacherGroup ptyTeacherGroup){
        try {
            QueryWrapper<PtyTeacherGroup> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",ptyTeacherGroup.getId());
            ptyTeacherGroupMapper.update(ptyTeacherGroup,queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除学生学生组表根据学生id
     */
    public boolean delPtyStuGroupRelInStu(String stu_id){
        try {
            QueryWrapper<PtyStuGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("stu_id",stu_id);
            ptyStuGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除学生学生组表根据学生组id
     */
    public boolean delPtyStuGroupRelInGroup(String group_id){
        try {
            QueryWrapper<PtyStuGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("stu_group_id",group_id);
            ptyStuGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    @Autowired
    private Notice notice;
    /**
     * 删除学生组根据教师id
     */
    public boolean delPtyStuGroupInTeacher(String teacher_id){
        try {
            List<PtyStuGroup> list = selectStuGroupInTeacher(teacher_id);
            for (PtyStuGroup ptyStuGroup : list) {
                notice.delPtyNoticeRelInGroup(ptyStuGroup.getId());
                delPtyStuGroupRelInGroup(ptyStuGroup.getId());
            }
            QueryWrapper<PtyStuGroup> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teacher_id",teacher_id);
            ptyStuGroupMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除学生组根据学生组id
     */
    public boolean delPtyStuGroupInId(String group_id){
        try {
            notice.delPtyNoticeRelInGroup(group_id);
            delPtyStuGroupRelInGroup(group_id);
            QueryWrapper<PtyStuGroup> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",group_id);
            ptyStuGroupMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 根据学生id和学生组id删除学生学生组表
     */
    public boolean delPtyStuGroupRel(String group_id,String stu_id){
        try {
            QueryWrapper<PtyStuGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("stu_group_id",group_id).in("stu_id",stu_id);
            ptyStuGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除教师教师组表根据教师id
     */
    public boolean delPtyTeacherGroupRelInTeacher(String teacher_id){
        try {
            QueryWrapper<PtyTeacherGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teacher_id",teacher_id);
            ptyTeacherGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除教师教师组表根据教师组id
     */
    public boolean delPtyTeacherGroupRelInGroup(String group_id){
        try {
            QueryWrapper<PtyTeacherGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teacher_group_id",group_id);
            ptyTeacherGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 删除教师组根据教师组id
     */
    public boolean delPtyTeacherGroupInId(String id){
        try {
            delPtyTeacherGroupRelInGroup(id);
            QueryWrapper<PtyTeacherGroup> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("id",id);
            ptyTeacherGroupMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
    /**
     * 根据教师id和教师组id删除教师教师组表
     */
    public boolean delPtyTeacherGroupRel(String group_id,String teacher_id){
        try {
            QueryWrapper<PtyTeacherGroupRel> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("teacher_group_id",group_id).in("teacher_id",teacher_id);
            ptyTeacherGroupRelMapper.delete(queryWrapper);
        }catch (Exception e){
            return false;
        }
        return true;
    }
}
